/*实现级联勾选开始*/
var nodeCheckedSilent = false;
function nodeChecked (event, node){
    if(nodeCheckedSilent){
        return;
    }
    nodeCheckedSilent = true;
    checkAllParent(node);
    checkAllSon(node);
    nodeCheckedSilent = false;
}

var nodeUncheckedSilent = false;
function nodeUnchecked  (event, node){
    if(nodeUncheckedSilent)
        return;
    nodeUncheckedSilent = true;
    uncheckAllParent(node);
    uncheckAllSon(node);
    nodeUncheckedSilent = false;
}

//选中全部父节点
function checkAllParent(node){
    $('#tree').treeview('checkNode',node.nodeId,{silent:true});
    var parentNode = $('#tree').treeview('getParent',node.nodeId);
    if(!("nodeId" in parentNode)){
        return;
    }else{
        checkAllParent(parentNode);
    }
}
//取消全部父节点
function uncheckAllParent(node){
    $('#tree').treeview('uncheckNode',node.nodeId,{silent:true});
    var siblings = $('#tree').treeview('getSiblings', node.nodeId);
    var parentNode = $('#tree').treeview('getParent',node.nodeId);
    if(!("nodeId" in parentNode)) {
        return;
    }
    var isAllUnchecked = true;  //是否全部没选中
    for(var i in siblings){
        if(siblings[i].state.checked){
            isAllUnchecked=false;
            break;
        }
    }
    if(isAllUnchecked){
        uncheckAllParent(parentNode);
    }

}

//级联选中所有子节点
function checkAllSon(node){
    $('#tree').treeview('checkNode',node.nodeId,{silent:true});
    if(node.nodes!=null&&node.nodes.length>0){
        for(var i in node.nodes){
            checkAllSon(node.nodes[i]);
        }
    }
}
//级联取消所有子节点
function uncheckAllSon(node){
    $('#tree').treeview('uncheckNode',node.nodeId,{silent:true});
    if(node.nodes!=null&&node.nodes.length>0){
        for(var i in node.nodes){
            uncheckAllSon(node.nodes[i]);
        }
    }
}



function prepareGrant() {

    var rows=$("#role").bootstrapTable("getSelections");
    if(rows.length==0){
        swal({
           title:"信息提示",
            text:"请选择一项角色授权",
            type:"warning",
            confirmButtonText:"确定"
        });
        return;
    }
    var url=ctx+"permission/permission/tree";
    var params={roleId:rows[0].id};
    $.getJSON(url,params,function(arr){
        $("#tree").treeview({
            data:arr,
            multiSelect:false,
            showCheckbox:true,
            onNodeChecked:nodeChecked,
            onNodeUnchecked:nodeUnchecked
        });

        $("#grant").dialog("open");
    });
}


$(function(){
   $("#grant").dialog({
      title:"角色授权",
       btnName:"授权",
       btnBind:function () {
           var rows=$("#role").bootstrapTable("getSelections");
           var params="roleId="+rows[0].id;
           var arr=$("#tree").treeview("getChecked");
           $(arr).each(function () {
              params=params+"&mids="+this.id;
           });
           $.post(ctx+"permission/permission/grant",params,function (msg) {
               toastr.success(msg);
               $("#grant").dialog("close");
               $("#role").bootstrapTable("uncheckAll");
           },'text');
       }
   });
});